package org.softmed.rest.admin.httpMethods;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.softmed.rest.admin.interfaces.CreateListener;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.interfaces.FileHandler;
import org.softmed.rest.admin.interfaces.PathChangedListener;
import org.softmed.rest.admin.interfaces.RemoveListener;
import org.softmed.rest.admin.interfaces.SelectionPathChanged;
import org.softmed.swing.IconManager;

public class MethodActionPanel extends JPanel implements ActionListener,
		PathChangedListener, SelectionPathChanged {

	EventListener addResourceListener;
	RemoveListener removeListener;
	CreateListener createListener;

	int width = 20;
	int height = 20;

	JLabel msg = new JLabel();

	JLabel validateError = new JLabel(IconManager.warning); // OK
	JLabel changed = new JLabel(IconManager.changed); // OK

	JButton create = new JButton(IconManager.newDocument); // OK
	JButton retrieve = new JButton(IconManager.leftArrow); // OK
	JButton open = new JButton(IconManager.openDocument); // OK
	JButton validateFiles = new JButton(IconManager.validate); // OK

	JButton updateOnServer = new JButton(IconManager.updateToServer);

	FileHandler fileHandler;

	// JButton remove = new JButton(IconManager.remove); // OK

	public MethodActionPanel() {
		// setBackground(Color.BLACK);
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

		setPreferredSize(new Dimension(540, 30));
		setMaximumSize(new Dimension(540, 30));
		setMinimumSize(new Dimension(540, 30));

		addButton(create);
		addButton(retrieve);
		addButton(open);

		// addButton(validateFiles);
		// addButton(updateOnServer);

		// addButton(remove);
		//
		// remove.setEnabled(true);
		add(msg);

		add(Box.createHorizontalGlue());

		add(validateError);
		validateError.setVisible(false);
		// add(changed);

		create
				.setToolTipText("Create new groovy file on the file browser's currently selected directory");
		retrieve
				.setToolTipText("Get the file path from the browser's currently selected file");
		open.setToolTipText("Open current groovy file");
		updateOnServer
				.setToolTipText("Forces the handler to be updated on the server");

		validateFiles
				.setToolTipText("Checks for the existence of the handler file on the server");

		validateError.setToolTipText("There was an error validating the file.");
		changed
				.setToolTipText("The configuration has changed from the currently stored version.");

		// create.setEnabled(true);
	}

	protected void addButton(JButton button) {
		resizeButton(button, width, height);
		button.addActionListener(this);
		button.setEnabled(false);
		add(button);
	}

	protected void resizeButton(JButton button, int width, int height) {
		button.setPreferredSize(new Dimension(width, height));
		button.setMaximumSize(new Dimension(width, height));
		button.setMinimumSize(new Dimension(width, height));
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();

		if (source == retrieve) {
			fileHandler.retrieveFilePath();
		} else if (source == create) {
			createListener.created();
		}
		// else
		// if (source == remove) {
		// removeListener.remove(this);
		// }

	}

	public EventListener getAddResourceListener() {
		return addResourceListener;
	}

	public void setAddResourceListener(EventListener addResourceListener) {
		this.addResourceListener = addResourceListener;
	}

	public RemoveListener getRemoveListener() {
		return removeListener;
	}

	public void setRemoveListener(RemoveListener removeListener) {
		this.removeListener = removeListener;
	}

	public void selectionPathChanged(boolean directory, String name,
			String parent) {
		retrieve.setEnabled(false);
		create.setEnabled(false);

		if (name == null) {
			return;
		} else if (!directory) {
			retrieve.setEnabled(true);
		}

		create.setEnabled(true);

	}

	public FileHandler getFileHandler() {
		return fileHandler;
	}

	public void setFileHandler(FileHandler fileHandler) {
		this.fileHandler = fileHandler;
	}

	@Override
	public void pathChanged(String path) {
		open.setEnabled(false);
		validateFiles.setEnabled(false);

		if (path == null) {
		} else if (path.endsWith(".groovy")) {
			open.setEnabled(true);

		}

		validateFiles.setEnabled(true);

	}

	public CreateListener getCreateListener() {
		return createListener;
	}

	public void setCreateListener(CreateListener createListener) {
		this.createListener = createListener;
	}
}
